home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / nurb_polyg / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.3 KB  |  104 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #include "nurbs.h"
  6. #include "drawing.h"
  7.  
  8. extern void MakeWindow( void );        /* External system routine to create a window */
  9.  
  10. void (*DrawTriangle)();            /* Pointer to triangle drawing function */
  11.  
  12. /* This generates the NURB surface for a torus, centered about the origin */
  13.  
  14. static NurbSurface *
  15. generateTorus(double majorRadius, double minorRadius)
  16. {
  17.     /* These define the shape of a unit torus centered about the origin. */
  18.     double xvalues[] = { 0.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 0.0 };
  19.     double yvalues[] = { 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0 };
  20.     double zvalues[] = { 0.0, 1.0, 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0 };
  21.     double offsets[] = { -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 0.0, -1.0, -1.0 };
  22.  
  23.     /* Piecewise Bezier knot vector for a quadratic curve with four segments */
  24.     long knots[] = { 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4 };
  25.  
  26.     long i, j;
  27.  
  28.     double r2over2 = sqrt( 2.0 ) / 2.0;
  29.     double weight;
  30.  
  31.     NurbSurface * torus = (NurbSurface *) malloc( sizeof(NurbSurface) );
  32.     CHECK( torus );
  33.  
  34.     /* Set up the dimension and orders of the surface */
  35.  
  36.     torus->numU = 9;        /* A circle is formed from nine points */
  37.     torus->numV = 9;
  38.     torus->orderU = 3;        /* Quadratic in both directions */
  39.     torus->orderV = 3;
  40.  
  41.     /* After the dimension and orders are set, AllocNurb creates the dynamic
  42.      * storage for the control points and the knot vectors */
  43.  
  44.     AllocNurb( torus, NULL, NULL );
  45.  
  46.     for (i = 0; i < 9; i++)
  47.     {
  48.     for (j = 0; j < 9; j++)
  49.     {
  50.         weight = ((j & 1) ? r2over2 : 1.0) * ((i & 1) ? r2over2 : 1.0);
  51.         /* Notice how the weights are pre-multiplied with the x, y and z values */
  52.         torus->points[i][j].x = xvalues[j]
  53.                     * (majorRadius + offsets[i] * minorRadius) * weight;
  54.         torus->points[i][j].y = yvalues[j]
  55.                     * (majorRadius + offsets[i] * minorRadius) * weight;
  56.         torus->points[i][j].z = (zvalues[i] * minorRadius) * weight;
  57.         torus->points[i][j].w = weight;
  58.     }
  59.     }
  60.  
  61.     /* The knot vectors define piecewise Bezier segments (the same in both U and V). */
  62.  
  63.     for (i = 0; i < torus->numU + torus->orderU; i++)
  64.     torus->kvU[i] = torus->kvV[i] = (double) knots[i];
  65.  
  66.     return torus;
  67. }
  68.  
  69. /* These drawing routines assume a window of around 400x400 pixels */
  70.  
  71. void
  72. ScreenProject( Point4 * worldPt, Point3 * screenPt )
  73. {
  74.     screenPt->x = worldPt->x / worldPt->w * 100 + 200;
  75.     screenPt->y = worldPt->y / worldPt->w * 100 + 200;
  76.     screenPt->z = worldPt->z / worldPt->w * 100 + 200;
  77. }
  78.  
  79. static void
  80. LineTriangle( SurfSample * v0, SurfSample * v1, SurfSample * v2 )
  81. {
  82.     MoveTo( (short) (v0->point.x * 100 + 200), (short) (v0->point.y * 100 + 200) );
  83.     LineTo( (short) (v1->point.x * 100 + 200), (short) (v1->point.y * 100 + 200) );
  84.     LineTo( (short) (v2->point.x * 100 + 200), (short) (v2->point.y * 100 + 200) );
  85.     LineTo( (short) (v0->point.x * 100 + 200), (short) (v0->point.y * 100 + 200) );
  86. }
  87.  
  88. main()
  89. {
  90.     NurbSurface * torus;
  91.  
  92.     MakeWindow();        /* Create a window on the screen */
  93.  
  94.     /* Set up the subdivision tolerance (facets span about two pixels) */
  95.     SubdivTolerance = 2.0;
  96.  
  97.     DrawTriangle = LineTriangle;
  98.  
  99.     torus = generateTorus( 1.3, 0.3 );
  100.  
  101.     DrawSubdivision( torus );
  102. /*  DrawEvaluation( torus );   */   /* Alternate drawing method */
  103. }
  104.